package mwa.cs.li.ctrl;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import mwa.cs.util.Mail;
import mwa.cs.dao.UserLoginDAO;
import mwa.cs.database.Database;
import mwa.cs.model.MailProperties;






/**
 * Servlet implementation class IdentifyUserSrvl
 */
public class IdentifyUserSrvl extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public IdentifyUserSrvl() {
		super();
		// TODO Auto-generated constructor stub
	}

	public void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		Database db = new Database();
		Mail voMail = new Mail();
		MailProperties voMailProperties = new MailProperties();
		UserLoginDAO voUserLoginDAO = new UserLoginDAO(db);
		Map<String, Object> voMap = new HashMap<String, Object>();
		String paUserName = request.getParameter("username");
		String paPageContext = request.getParameter("contextPath");
		String page ="";
		request.setAttribute("UserName", paUserName);
		voMap = voUserLoginDAO.findUserByUserName(paUserName);
		if (voMap == null) {
			if(paUserName!=null){
				request.setAttribute("vaForgetPasswordMessageError",
						"ไม่มีชื่อผู้ใช้ในระบบ");
			}
			
			page = "index.jsp";
			
			request.setAttribute("vaTypeForm", "forgetPassword");
		} else {
			voMap = voUserLoginDAO.findUserByUserName(paUserName);
			
			String vaMailSubject = "มีคนขอรหัสผ่านใหม่ของบัญชีผู้ใช้ ระบบบริหารงานประปา ของคุณ";
			String vaMailtext = "มีคนขอให้คุณตั้งรหัสผ่านบัญชีผู้ใช้  ระบบบริหารงานประปา  ของคุณใหม่\n"
					+ "<!DOCTYPE html>"
					+ "<html>"
					+ "<head>"
					+ "<meta http-equiv='Content-Type' content='text/html; charset=UTF-8' />"
					+ "<meta content='width=device-width, initial-scale=1.0' name='viewport' />"
					+ "</head>"
					+ "<body>"
					+ "<a href='"+paPageContext+"/recover/RecoverPasswordSrvl?UserId="+voMap.get("USER_ID").toString()+"&Key="+voMap.get("PASSWORD_KEY").toString()+"'>"
					+ "<button style='font-family:Helvetica Neue,Helvetica,Lucida Grande,tahoma,verdana,arial,sans-serif;font-weight:bold;font-size:14px;color:#ffffff;background-color: #428bca;border-color: #357ebd;text-shadow:0 1px 0 #415686;display: inline-block;margin-bottom: 0;font-weight: 400;text-align: center;vertical-align: middle;cursor: pointer;background-image: none;border: 1px solid transparent;white-space: nowrap;padding: 6px 12px;font-size: 14px;line-height: 1.428571429;border-radius: 4px;-webkit-user-select: none;-moz-user-select: none;-ms-user-select: none;-o-user-select: none;user-select: none;'>"
					+ "คลิ๊กที่นี่เพื่อกำหนดรหัสผ่านใหม่"
					+ "</button>"
					+ "</a>"
					+ "</body>"
					+ "</html>";

			voMailProperties.setVaMailAuth("true");
			voMailProperties.setVaMailHost("mail.scapsolutions.com");
			voMailProperties.setVaMailPort("25");
			voMailProperties.setVaMailReceiver(voMap.get("EMAIL_ADDRESS").toString());
			voMailProperties.setVaMailSender("chatrapee.octo@gail.com");
			voMailProperties.setVaMailSenderPassword("Welcome-1");
			voMailProperties.setVaMailSubject(vaMailSubject);
			voMailProperties.setVaMailText(vaMailtext);
			voMail.sendMail(voMailProperties);
			page = "index.jsp";
			request.setAttribute("vaTypeForm", "IdentifyUser");
			request.setAttribute("vaStatus", "Forget");
			request.setAttribute("UserName", null);
			System.out.println("Success");
		}
		RequestDispatcher rd = request.getRequestDispatcher(page);
		rd.forward(request, response);
		
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

}
